package com.caigou.comm.base.container

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import com.caigou.comm.R
import com.caigou.comm.base.utils.DialogUtil
import com.caigou.comm.base.utils.StatusBarUtil
import com.caigou.library.statusbar.NotchScreenManager
import java.util.*

/**
 * @author #Suyghur.
 * Created on 2021/09/13
 */
open class BaseActivity : AppCompatActivity() {

    protected open var backgroundColor = Color.TRANSPARENT
    protected val fragmentStack: LinkedList<String> = LinkedList()
    protected open var currentFragment = ""

    protected open var containerId = 0

    protected open val mDialog by lazy {
        DialogUtil.getLoadingDialog(this, "")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        overridePendingTransition(R.anim.comm_popup, R.anim.comm_push)

        backgroundColor = resources.getColor(R.color.caigou_white_gray)
        NotchScreenManager.getInstance().setDisplayInNotch(this)
        initColor()
        observeViewModel()
    }

    override fun finish() {
        super.finish()
        overridePendingTransition(R.anim.comm_popup, R.anim.comm_push)
    }

    open fun initColor() {
        StatusBarUtil.setColor(this, backgroundColor, 0)
        if (this.supportActionBar != null) {
            this.supportActionBar?.setBackgroundDrawable(ColorDrawable(backgroundColor))
        }
        window.setBackgroundDrawable(ColorDrawable(backgroundColor))
    }


    open fun observeViewModel() {

    }

    open fun switchFragment(tag: String) {

    }

    open fun switchFragment(fragment: BaseFragment, target: String) {
        if (fragmentStack.contains(target)) {
            // 栈里最后一个与tag相同才执行删除
            if (fragmentStack.size > 1 && !fragmentStack.last.equals(target)) {
                fragmentStack.removeLast()
            }
        } else {
            fragmentStack.add(target)
        }

        val transaction = supportFragmentManager.beginTransaction()
        transaction.setCustomAnimations(R.anim.comm_popup, R.anim.comm_push)
        var targetFragment = supportFragmentManager.findFragmentByTag(currentFragment)
        if (targetFragment != null && target != currentFragment) {
            targetFragment.onPause()
            transaction.hide(targetFragment)
        }
        targetFragment = supportFragmentManager.findFragmentByTag(target)
        if (targetFragment == null) {
            transaction.add(containerId, fragment, target)
        } else {
            targetFragment.onResume()
            transaction.show(targetFragment)
        }
        currentFragment = target
        transaction.commitAllowingStateLoss()
    }

    private fun hideBar() {
        // The UI options currently enabled are represented by a bitfield.
        // getSystemUiVisibility() gives us that bitfield.
        val uiOptions = window.decorView.systemUiVisibility
        var newUiOptions = uiOptions
        val isImmersiveModeEnabled = uiOptions or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY == uiOptions
        if (!isImmersiveModeEnabled) {
            if (Build.VERSION.SDK_INT >= 14) {
                newUiOptions = newUiOptions or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            }
            if (Build.VERSION.SDK_INT >= 16) {
                newUiOptions = newUiOptions or View.SYSTEM_UI_FLAG_FULLSCREEN
            }
            if (Build.VERSION.SDK_INT >= 18) {
                newUiOptions = newUiOptions or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            }
            window.decorView.systemUiVisibility = newUiOptions
        }
    }


    protected fun isShouldHideInput(v: View?, event: MotionEvent): Boolean {
        if (v is EditText) {
            val leftTop = intArrayOf(0, 0)
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop)
            val left = leftTop[0]
            val top = leftTop[1]
            val bottom = top + v.getHeight()
            val right = left + v.getWidth()
            // 点击的是输入框区域，保留点击EditText的事件
            return event.x <= left || event.x >= right || event.y <= top || event.y >= bottom
        }
        return false
    }
}